package com.hgd.security.evp.biz.rest;

import com.hgd.security.evp.auth.entity.CustomUserDetails;
import com.hgd.security.evp.biz.biz.EvpMenuBiz;
import com.hgd.security.evp.biz.entity.EvpMenu;
import com.hgd.security.evp.biz.vo.EMenuVo;
import com.hgd.security.evp.biz.vo.MenuTree;
import com.hgd.security.evp.common.util.ObjectResultResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("baseMenu")
@Api(tags = "菜单管理")
@Log4j
public class EMenuController {

    @Autowired
    private EvpMenuBiz eMenuBiz;

    @PostMapping("/instance")
    @ApiOperation("添加菜单")
    public ObjectResultResponse<Void> addEMenu(@RequestBody @ApiParam(value = "待添加对象") EvpMenu eMenu,
        BindingResult bindingResult) {
        log.info("/baseMenu/instance==>添加菜单");
        ObjectResultResponse<Void> oResult = new ObjectResultResponse<>();
        if (bindingResult.hasErrors()) {
            oResult.setStatus(400);
            oResult.setMessage(bindingResult.getAllErrors().get(0).getDefaultMessage());
            return oResult;
        }

        oResult = eMenuBiz.addEmenu(eMenu);
        return oResult;
    }

    @GetMapping("/tree")
    @ApiOperation("获取菜单树")
    public List<MenuTree> eMenuTree() {
        log.info("/baseMenu/tree==>获取菜单树");
        return eMenuBiz.getMenuTree();
    }

    @GetMapping("/{id}")
    public ObjectResultResponse<EMenuVo> getMenuListById(@PathVariable("id") @ApiParam("待删除组ID") Integer id) {
        return eMenuBiz.getMenuListById(id);
    }

    @PutMapping("/instance")
    @ApiOperation("更新菜单")
    public ObjectResultResponse<Void> updateEMenu(@RequestBody @ApiParam(value = "待更新对象") EvpMenu eMenu,
        BindingResult bindingResult) {
        log.info("/baseMenu/instance==>更新菜单");
        ObjectResultResponse<Void> oResult = new ObjectResultResponse<>();
        if (bindingResult.hasErrors()) {
            oResult.setStatus(400);
            oResult.setMessage(bindingResult.getAllErrors().get(0).getDefaultMessage());
            return oResult;
        }

        oResult = eMenuBiz.updateEmenu(eMenu);
        return oResult;
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除单个对象")
    public ObjectResultResponse<Void> delMenuListById(@PathVariable("id") @ApiParam("待删除对象ID") Integer id) {
        return eMenuBiz.delMenuListById(id);
    }

    @GetMapping("/{groupId}/list")
    @ApiOperation("获取某组下的菜单")
    public List<EvpMenu> getMenuOfGroup(@PathVariable(value = "groupId") Integer groupId) {
        return eMenuBiz.getMenuOfGroup(groupId);
    }

    @GetMapping("/user/tree")
    @ApiOperation("获取某用户的菜单树")
    public List<MenuTree> userEMenuTree(
        Authentication authentication
    ) {
        CustomUserDetails principal = (CustomUserDetails) (authentication.getPrincipal());
        log.info("/user/{userId}/tree==>获取某用户的菜单树");
        return eMenuBiz.getAuthorityMenu(principal.getUserId());
    }
}